<template>
    <el-button :icon="icon" @click="goBack" :type="type">
        <slot/>
    </el-button>
</template>

<script>
export default {
    props:{
        type:{
            type:String,
            //default:'info'
        },
        icon:{
            type:String,
            default:'el-icon-back'
        },
        reGetDataFlag:{
            type:Boolean,
            default:false
        },
    },
    computed: {
        visitedViews() {
            return this.$store.state.tagsView.visitedViews
        },
        preVisitedViews() {
            return this.$store.state.tagsView.preVisitedViews
        },
        routes() {
            return this.$store.state.permission.routes
        }
    },
    methods:{
        goBack(){
            !this.reGetDataFlag && this.$store.commit('tagsView/SET_REGETDATA','') // 清空刷新方法
            this.closeSelectedTag()
        },
        isActive(route) {
            return route.path === this.$route.path
        },
        closeSelectedTag() {
            const view = this.$route
            this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
                this.$store.dispatch('tagsView/addPreVisitedView', this.visitedViews)
                if (this.isActive(view)) {
                    this.toLastView(visitedViews, view)
                }
            })
        },
        toLastView(visitedViews, view) {
            const latestView = visitedViews.slice(-1)[0]
            this.$router.back()
        },
    }
}
</script>

<style>

</style>